package xj.graph2d.app.project;

import static xj.app.BaseApplicationFrame.getResourceString;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Date;

import javax.swing.BorderFactory;
import javax.swing.Icon;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import xj.graph2d.DocumentException;
import xj.graph2d.app.DrawAction;
import xj.graph2d.app.DrawApp;
import xj.graph2d.dialog.DialogPanel;
import xj.graph2d.dialog.TitledDialogPanel;

/*
 *  To create a new project, or edit properties of an existing project 
 */
public class ProjectPropertiesListener 
  extends DrawAction 
  implements ActionListener {

  class ProjectOptionPanel extends DialogPanel {

    public ProjectOptionPanel(String name) {
      super(name);
      JLabel nameLabel = new JLabel(getResourceString("ProjectName.text") + " ");
      nameField = new JTextField();

      JLabel typeLabel = new JLabel(getResourceString("ProjectType.text") + " ");
      typeField = new JComboBox();
      typeField.addItem(ProjectType.UML);
      typeField.addItem(ProjectType.Default);
      JLabel authorLabel = new JLabel(getResourceString("Author.text") + " ");
      authorField = new JTextField();
      JLabel titleLabel = new JLabel(getResourceString("Title.text") + " ");
      titleField = new JTextField();
      JLabel orgLabel = new JLabel(getResourceString("Organization.text") + " ");
      orgField = new JTextField();

      JLabel descriptionLabel = new JLabel(getResourceString("Description.text") + " ");
      descriptionLabel.setVerticalAlignment(SwingConstants.TOP);
      descriptionField = new JTextArea(4, 40);
      descriptionField.setBorder(BorderFactory.createEtchedBorder());

      JLabel ctimeLabel1 = new JLabel(getResourceString("Create.text") + " ");
      ctimeLabel = new JLabel();

      addRow(nameLabel, nameField);
      addRow(typeLabel, typeField);
      addRow(titleLabel, titleField);
      addRow(authorLabel, authorField);
      addRow(orgLabel, orgField);
      addRow(descriptionLabel, new JScrollPane(descriptionField));
      addRow(ctimeLabel1, ctimeLabel);

      setPreferredSize(new Dimension(400, 300));
      setOption();
    }

    @Override
    public void setOption() {
      ProjectManager projectManager = main.getProjectManager();
      Project project = projectManager.getCurrentProject();
      if (project != null && !newProject) {
	String name = project.getName();
	ProjectType type = project.getProjectType();
	String title = project.getTitle();
	String author = project.getAuthor();
	String org = project.getOrganization();
	String des = project.getDescription();
	nameField.setText(name);
	typeField.setSelectedItem(type);
	authorField.setText(author);
	titleField.setText(title);
	orgField.setText(org);
	descriptionField.setText(des);
	ctimeLabel.setText((new Date(project.getCreationTime())).toString());
      } else {
	nameField.setText(projectManager.getNewProjectName(null));
	typeField.setSelectedIndex(0);
	authorField.setText(null);
	titleField.setText(null);
	orgField.setText(null);
	descriptionField.setText(null);
	ctimeLabel.setText((new Date()).toString());
      }
    }

    @Override
    public void applyOption() {
      ProjectManager projectManager = main.getProjectManager();
      Project project = projectManager.getCurrentProject();
      String name = nameField.getText();
      String title = titleField.getText();
      String author = authorField.getText();
      String org = orgField.getText();
      String des = descriptionField.getText();
      ProjectType type = (ProjectType) typeField.getSelectedItem();
      if (project != null && !newProject) {
	project.setName(name);
      } else {
	project = new Project(main, name, type);
	if (type == ProjectType.UML) {
	  ProjectFolder mfolder = new ProjectFolder("Model",
						    ProjectFolder.Type.UML_Model);
	  projectManager.addProjectItem(mfolder, project, false);
	  main.newProjectItem(mfolder, 
			      //projectManager.getNewDefaultItemName(),
			      mfolder.getNewItemName(projectManager.getDefaultItemName()),
			      projectManager.getDefaultItemType());
	  // projectManager.addProjectItem(item, mfolder, false);
	} else { 
	  main.newProjectItem(project, 
			      project.getNewItemName(project.getDefaultItemName()),
			      project.getDefaultItemType());
	}
	projectManager.addProject(project);
	if (main != null) {
	  main.initProjectModel(project);
	  project.setDefaultDir(main.getDefaultDir());
	  project.setWorkingDir(main.getDefaultDir());
	  try { 
	    project.saveProject();
	  } catch (IOException e) {
	    String msg1 = "Save project error: " + name;
	    String msg2 = e.getMessage();
	    main.displayMessage(msg1);
	    JOptionPane.showMessageDialog(this, msg1 + "\n" + msg2,
					  "Save Project Error", 
					  JOptionPane.ERROR_MESSAGE);
	  } catch (DocumentException e) {
	    String msg1 = "Save project error: " + name;
	    String msg2 = e.getMessage();
	    main.displayMessage(msg1);
	    JOptionPane.showMessageDialog(this, msg1 + "\n" + msg2,
					  "Save Project Error", 
					  JOptionPane.ERROR_MESSAGE);
	  }
	}
      }
      project.setProjectType(type);
      project.setAuthor(author);
      project.setTitle(title);
      project.setOrganization(org);
      project.setDescription(des);
    }

    protected JTextField nameField;

    protected JComboBox typeField;

    protected JTextField authorField;

    protected JTextField titleField;

    protected JTextField orgField;

    protected JTextArea descriptionField;

    protected JLabel ctimeLabel;

  }

  protected TitledDialogPanel projectOptionDialog = null;
  protected ProjectOptionPanel projectPanel = null;
  
  protected boolean newProject = false; // true: new project; false: edit existing project

  //protected String projectPanelTitle;
  protected String newProjectDialogTitle;
  protected String newProjectHeader;
  protected String[] newProjectText;
  protected String editProjectDialogTitle;
  protected String editProjectHeader;
  protected String[] editProjectText;
  protected Icon icon;

  @Override
  protected void init() { 
    newProjectDialogTitle = getResourceString("NewProjectDialog.title");
    newProjectHeader = getResourceString("NewProjectDialog.header");
    newProjectText = new String[] { 
	getResourceString("NewProjectDialog.text1"),
	getResourceString("NewProjectDialog.text2"),	
    };
    editProjectDialogTitle = getResourceString("EditProjectDialog.title");
    editProjectHeader = getResourceString("EditProjectDialog.header");
    editProjectText = new String[] { 
	getResourceString("EditProjectDialog.text1"),
	getResourceString("EditProjectDialog.text2"),	
    };
    icon = DrawApp.getIcon("NewProjectWizIcon");
  }
  
  public void actionPerformed(ActionEvent e) {
    // System.out.println("ProjectPropertiesListener.actionPerformed()");
    if (projectOptionDialog == null) {
      DialogPanel[] options = { 
	projectPanel = new ProjectOptionPanel(null), //projectPanelTitle), 
      };
      projectOptionDialog = new TitledDialogPanel(main.getJFrame(), 
						  JOptionPane.OK_CANCEL_OPTION, 
						  null, options);
    }
    String cmd = e.getActionCommand();
    if ("NewProject".equals(cmd)) {
      newProject = true;
      projectOptionDialog.setTitle(newProjectDialogTitle);
      projectOptionDialog.setTitlePaneText(newProjectHeader, newProjectText, icon);
    } else {
      newProject = false;
      projectOptionDialog.setTitle(editProjectDialogTitle);
      projectOptionDialog.setTitlePaneText(editProjectHeader, editProjectText, icon);
    }
    projectPanel.nameField.setEnabled(newProject);
    projectOptionDialog.setOptions();
    projectOptionDialog.showOptionDialog();
  }

}
